package com.ruoyi.loan.utils;

import cn.hutool.core.io.resource.ClassPathResource;
import com.ruoyi.common.utils.StringUtils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * @author coderFu
 * @create 2022/4/5
 */
@Component
public class FileUtils {

    // 基本的存储包名，后续可以添加推展，也可以直接传入自定义的

    public static final  String LOAN_IMG = "LOAN_IMG";

    public static final String LOAN_VOICE = "LOAN_VOICE";

    public static final String AVATAR = "AVATAR";

    public static InetAddress localHost;

    @Autowired
    private  ServerProperties serverProperties;


    static {
        try {
            localHost = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

    // 文件传输的大小限制

    private static  final  long FILE_SIZE = 15*1024*1024;

   static String selfPath = new ClassPathResource("static").getAbsolutePath();

    /**
     *  获取完整图片路径
     */
    public static String getAllImagePath(String path){
        return selfPath+ File.separator+path;
    }

    public static String upLoadFile(MultipartFile multipartFile , String baseName){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSS");

        String res = sdf.format(new Date());

        // 获取存储文件的路径，基于当前项目的文件夹
        String rootPath = getStoragePath(baseName);

        String originalFileName = multipartFile.getOriginalFilename();

        String newFileName = "";

        if (!StringUtils.isNotEmpty(originalFileName)){
            newFileName = res + UUID.randomUUID().toString()+".jpg";
        }else{
            newFileName = res + UUID.randomUUID().toString()+originalFileName;
        }

        //根据当前日期创建文件夹
        Calendar date = Calendar.getInstance();

        File dateDirs = new File(date.get(Calendar.YEAR)
                +File.separator+(date.get(Calendar.MONTH)+1)
                +File.separator+(date.get(Calendar.DATE)));

        String desFilePath = File.separator + dateDirs +File.separator+newFileName;

        File newFile = new File(rootPath+desFilePath);

        // 判断目标文件夹是否存在
        if (! newFile.getParentFile().exists()){
            newFile.getParentFile().mkdirs();
        }

        // 进行文件上传
        try {
            multipartFile.transferTo(newFile);
        }catch (IOException e){
            throw new RuntimeException("文件上传失败，请重新上传");
        }
        return baseName+desFilePath;
    }

    public static String getStoragePath(String baseName){
       return selfPath+"\\"+baseName+"";
    }

    public  String getShowFilePath(String path){
        return "http://"+localHost.getHostAddress()+":"+serverProperties.getPort()+"/"+path;
    }

    /**
     * 删除文佳
     */

    public static boolean deleteFile(String path){
        //获取完整路径
        String allPath = getAllImagePath(path);
        File file = new File(allPath);
        if (file.exists()){
            return file.delete();
        }
        return false;
    }

    /**
     * 检查图片文件是否合规
     * @param multipartFile
     * @return
     */
    public static boolean checkFileSize(MultipartFile multipartFile){
        return multipartFile.getSize() <= FILE_SIZE;
    }

}
